package com.jinlejia.bbs.ui.fun;

import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.View;

import com.jinlejia.bbs.R;
import com.jinlejia.bbs.adapter.TagAdapter;
import com.jinlejia.bbs.bean.base.TagBean;
import com.jinlejia.bbs.bean.base.TagGroupBean;
import com.jinlejia.bbs.databinding.BbsActivityChooseTagBinding;
import com.jinlejia.bbs.logic.mine.SaveUserTagLogic;
import com.jinlejia.bbs.widget.BBSToast;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class BBSChooseTagActivity extends AppCompatActivity {

    private BbsActivityChooseTagBinding binding;
    private SaveUserTagLogic logic;
    private ArrayList<TagBean> selectTags;
    private boolean save; //0:更新用户的标签 1:选择标签
    private boolean ignoreEnable = false;  //true 显示 跳过 false 不显示跳过，显示关闭
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.bbs_activity_choose_tag);
        selectTags = getIntent().getParcelableArrayListExtra("tags");
        this.save = getIntent().getBooleanExtra("save",false);
        this.ignoreEnable = getIntent().getBooleanExtra("ignore",false);

        if (ignoreEnable){
            binding.ignoreTextView.setVisibility(View.VISIBLE);
            binding.closeImageView.setVisibility(View.INVISIBLE);
        }else {
            binding.ignoreTextView.setVisibility(View.INVISIBLE);
            binding.closeImageView.setVisibility(View.VISIBLE);
        }
        binding.setEvent(this);
        logic = new SaveUserTagLogic(this);
        initView();
    }

    private void initView() {
        List<TagGroupBean> beans = new ArrayList<>();
        String[]groupName = {"资源熟悉","资源开发","撮合服务","签约谈判","签后服务"};
        String[]tag1 = {"跑盘","空看"};
        String[]tag2 = {"门店接待","其他开发","网络端口开发"};
        String[]tag3 = {"需求理解与匹配","邀约","高效带看","房源客源维护与议价"};
        String[]tag4 = {"签约前中后"};
        String[]tag5 = {"缴税","过户","物业交接"};
        for (int i = 0; i < 5; i++) {
            TagGroupBean group = new TagGroupBean();
            group.setGroupName(groupName[i]);
            ArrayList<TagBean> tags = new ArrayList<>();
            for (int j = 0; j < 4; j++) {
                TagBean tag = new TagBean();
                if (i==0){
                    if (j<tag1.length){
                        tag.setValueName(tag1[j]);
                    }
                }else if (i==1){
                    if (tag2.length>j){
                        tag.setValueName(tag2[j]);
                    }
                }else if (i==2){
                    if (tag3.length>j){
                        tag.setValueName(tag3[j]);
                    }
                }else if (i==3){
                    if (tag4.length>j){
                        tag.setValueName(tag4[j]);
                    }
                }else if (i==4){
                    if (tag5.length>j){
                        tag.setValueName(tag5[j]);
                    }
                }
                if (!"null".equalsIgnoreCase(tag.getValueName()) && tag.getValueName()!=null){
                    tags.add(tag);
                }
                if (selectTags!=null){
                    for (TagBean t:selectTags) {
                        if (t.equals(tag)){
                            tag.setSelected(true);
                            break;
                        }
                    }
                }

            }
            group.setTags(tags);
            beans.add(group);
        }
        LinearLayoutManager layoutManager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
        final TagAdapter adapter = new TagAdapter(this,beans);
        binding.recyclerView.setAdapter(adapter);
        binding.recyclerView.setLayoutManager(layoutManager);
        adapter.setListener(new TagAdapter.OnChangeListener() {
            @Override
            public void onChange(TagAdapter adapter) {
                binding.submitButton.setEnabled(true);
            }
        });
    }

    public void getSelectTags(View view){
        TagAdapter adapter = (TagAdapter) binding.recyclerView.getAdapter();
        ArrayList<TagBean> selectTags = adapter.getSelectTags();
        if (selectTags==null || selectTags.size()==0){
            BBSToast.showToast(this,"请至少选择一个标签",1500);
            return;
        }
        Object[] array = selectTags.toArray();
        String string = Arrays.toString(array);
        string = string.replace("[","").replace("]","").replace(" ","");
        if (!save){
            Intent data = new Intent();
            data.putExtra("data",string);
            setResult(RESULT_OK,data);
            finish();
        }else{
            final String finalString = string;
            logic.execute(string, new SaveUserTagLogic.LogicCallBack() {
                @Override
                public void onResult(boolean flag) {
                    if (flag){
                        Intent data = new Intent();
                        data.putExtra("data", finalString);
                        setResult(RESULT_OK,data);
                        finish();
                    }
                }
            });

        }


    }

    public void ignore(View view){
        this.finish();
    }

    public void finishView(View view){
        this.finish();
    }
}
